<title>商家管理</title>
  
<div class="layui-card layadmin-header">
  <div class="layui-breadcrumb" lay-filter="breadcrumb">
    <a lay-href="">主页</a>
    <a><cite>商家管理</cite></a>
    <a><cite>商家管理</cite></a>
  </div>
</div>

<div class="layui-fluid">
  <div class="layui-card">
    <div class="layui-form layui-card-header layuiadmin-card-header-auto" lay-filter="layadmin-merchfront-formlist">
      <div class="layui-form-item">
        <div class="layui-inline">
          <label class="layui-form-label">编号</label>
          <div class="layui-input-block">
            <input type="text" name="id" placeholder="请输入" autocomplete="off" class="layui-input">
          </div>
        </div>
        <div class="layui-inline">
          <label class="layui-form-label">昵称</label>
          <div class="layui-input-block">
            <input type="text" name="nickname" placeholder="请输入" autocomplete="off" class="layui-input">
          </div>
        </div>
        <div class="layui-inline">
          <label class="layui-form-label">openid</label>
          <div class="layui-input-block">
            <input type="text" name="openid" placeholder="请输入" autocomplete="off" class="layui-input">
          </div>
        </div>
        <div class="layui-inline">
          <label class="layui-form-label">性别</label>
          <div class="layui-input-block">
            <select name="gender">
              <option value="">不限</option>
              <option value="1">男</option>
              <option value="0">女</option>
            </select>
          </div>
        </div>
        <div class="layui-inline">
          <label class="layui-form-label">手机号</label>
          <div class="layui-input-block">
            <input type="text" name="mobile" placeholder="请输入" autocomplete="off" class="layui-input">
          </div>
        </div>
        <div class="layui-inline">
          <button class="layui-btn layuiadmin-btn-merch" lay-submit lay-filter="LAY-user-front-search">
            <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
          </button>
        </div>
      </div>
    </div>
    
    <div class="layui-card-body">
      <div style="padding-bottom: 10px;">
        <button class="layui-btn layui-btn-danger layuiadmin-btn-merch" data-type="batchdel">删除</button>
        <button class="layui-btn layuiadmin-btn-merch" data-type="add">添加</button>
      </div>
      
      <table id="LAY-merch-manage" lay-filter="LAY-merch-manage"></table>
      <script type="text/html" id="imgTpl"> 
        <img style="display:inline-block;width:30px;height:30px" src= {{ d.avatar }} >
        {{ d.nickname }}
      </script> 
      <script type="text/html" id="timeTpl"> 
        {{# var date = new Date(d.createtime * 1000);
          var dt = date.getFullYear() + '-';
          dt += (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
          dt += (Array(2).join(0) + date.getDate()).slice(-2) + ' ';
          dt += (Array(2).join(0) + date.getHours()).slice(-2) + ':';
          dt += (Array(2).join(0) + date.getMinutes()).slice(-2) + ':';
          dt += (Array(2).join(0) + date.getSeconds()).slice(-2); }}
        {{ dt }}
      </script> 
      <script type="text/html" id="endtimeTpl">
        {{# if(d.endtime==0){ }}
        永久
        {{# }else{ }}
        {{# var date = new Date(d.endtime * 1000);
          var dt = date.getFullYear() + '-';
          dt += (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
          dt += (Array(2).join(0) + date.getDate()).slice(-2) + ' '; }}
        {{ dt }}
        {{# } }}
      </script> 
      <script type="text/html" id="table-merch-webuser">
        <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
        <a class="layui-btn layui-btn-xs" lay-event="sale"><i class="layui-icon layui-icon-form"></i>活动</a>
      </script>
    </div>
  </div>
</div>

<script>
layui.use('merch', layui.factory('merch')).use(['admin', 'merch', 'table'], function(){
  var $ = layui.$
  ,admin = layui.admin
  ,view = layui.view
  ,table = layui.table
  ,form = layui.form;
  
  form.render(null, 'layadmin-merchfront-formlist');
  
  //监听搜索
  form.on('submit(LAY-user-front-search)', function(data){
    var params = [];
    for (let key in data.field) {
      params.push(key+'='+data.field[key]);
    }
    //执行重载
    table.reload('LAY-merch-manage', {
      url: layui.setter.baseUrl+"merch/index?"+params.join('&')
    });
  });

  //事件
  var active = {
    batchdel: function(){
      var checkStatus = table.checkStatus('LAY-merch-manage')
      ,checkData = checkStatus.data; //得到选中的数据

      if(checkData.length === 0){
        return layer.msg('请选择数据');
      }
      
      layer.confirm('确定删除吗？', function(index) {
        var ids = [];
        checkData.forEach((item,index)=>{
          ids.push(item.id);
        });
        $.post(
            layui.setter.baseUrl+'merch/del',
            {id: ids.join(',')},
            function(ret){
                var {code,msg,data} = ret;
                layer.alert(msg);
                if(code==0){
                    table.reload('LAY-merch-manage');
                }
            }
        );
      });
    }
    ,add: function(){
      admin.popup({
        title: '添加商家'
        ,area: [$(window).width()*0.618+'px', $(window).height()*0.9+'px']
        ,id: 'LAY-popup-merch-add'
        ,success: function(layero, index){
          view(this.id).render('merch/add');
        }
      });
    }
  };
  
  $('.layui-btn.layuiadmin-btn-merch').on('click', function(){
    var type = $(this).data('type');
    active[type] ? active[type].call(this) : '';
  });
});
</script>